package com.ruoyi.cinema.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.cinema.domain.Area;
import com.ruoyi.cinema.mapper.AreaMapper;
import com.ruoyi.cinema.service.IAreaService;
import com.ruoyi.common.core.domain.FrontTreeObj;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 区域Service业务层处理
 *
 * @author HHAOYU
 * @date 2020-12-07
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements IAreaService {
    @Autowired
    private AreaMapper areaMapper;

    /**
     * 查询区域
     *
     * @param id 区域ID
     * @return 区域
     */
    @Override
    public Area selectAreaById(Long id)
    {
        return areaMapper.selectAreaById(id);
    }

    /**
     * 查询区域列表
     *
     * @param area 区域
     * @return 区域
     */
    @Override
    public List<Area> selectAreaList(Area area)
    {
        return areaMapper.selectAreaList(area);
    }

    @Override
    public List<FrontTreeObj> selectAreaListForTreeSelect(Area area){
        return areaMapper.selectAreaListForTreeSelect(area);
    }
    /**
     * 新增区域
     *
     * @param area 区域
     * @return 结果
     */
    @Override
    public int insertArea(Area area)
    {

        Area parent =  areaMapper.selectAreaById(area.getParentId());
        if(parent != null){
            area.setAncestors(parent.getAncestors()+","+area.getParentId());
        }else {
            area.setAncestors(String.valueOf(area.getParentId()));
        }

        area.setCreateTime(DateUtils.getNowDate());
        return areaMapper.insertArea(area);
    }

    /**
     * 修改区域
     *
     * @param area 区域
     * @return 结果
     */
    @Override
    public int updateArea(Area area)
    {
        area.setUpdateTime(DateUtils.getNowDate());
        return areaMapper.updateArea(area);
    }

    /**
     * 批量删除区域
     *
     * @param ids 需要删除的区域ID
     * @return 结果
     */
    @Override
    public int deleteAreaByIds(Long[] ids)
    {
        return areaMapper.deleteAreaByIds(ids);
    }

    /**
     * 删除区域信息
     *
     * @param id 区域ID
     * @return 结果
     */
    @Override
    public int deleteAreaById(Long id)
    {
        return areaMapper.deleteAreaById(id);
    }
}
